package com.enterprise.controller.api.index;

import com.enterprise.controller.api.banner.vo.BannerResp;
import com.enterprise.controller.api.caseapi.vo.CaseResp;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.controller.api.cust.vo.CustserviceResp;
import com.enterprise.controller.api.index.vo.IndexResp;
import com.enterprise.controller.api.news.vo.NewsResp;
import com.enterprise.controller.api.team.vo.TeamResp;
import com.enterprise.core.SystemManage;
import com.enterprise.entity.*;
import com.enterprise.service.*;
import com.enterprise.util.AddressUtil;
import com.enterprise.util.RequestHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@RequestMapping("/api/index")
@Controller("apiIndexController")
public class IndexController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private IndexImgService bannerService;

    @Autowired
    private CaseService caseService;

    @Autowired
    private CustserviceService custserviceService;

    @Autowired
    private TeamService teamService;

    @Autowired
    LogService logService;

    @ResponseBody
    @RequestMapping("/icp")
    public BaseResponse index(){
        String icpOne = SystemManage.getInstance().getSystemSetting().getIcp();
        String icpTwo = SystemManage.getInstance().getSystemSetting().getStatisticsCode();
        Map<String,String> map = new HashMap<>();
        map.put("icpOne",icpOne);
        map.put("icpTwo",icpTwo);
        return BaseResponse.success(map);
    }

    @ResponseBody
    @RequestMapping("/list")
    public BaseResponse<IndexResp> list(){
        BaseResponse response = new BaseResponse();
        IndexResp indexResp = new IndexResp();
        // 组装信息
        List<News> newsList = newsService.selectList(new News());
        List<NewsResp> newsResps = new ArrayList<>();
        for (News news:newsList) {
            NewsResp newsResp = new NewsResp();
            BeanUtils.copyProperties(news,newsResp);
            newsResp.setNewsimgurl(BaseResponse.httpstr(newsResp.getNewsimgurl()));
            newsResps.add(newsResp);
        }
        indexResp.setNewsList(newsResps);

        // banner
        List<IndexImg> bannerList = bannerService.selectList(new IndexImg());
        List<BannerResp> bannerResps = new ArrayList<>();
        for (IndexImg banner:bannerList) {
            BannerResp bannerResp = new BannerResp();
            BeanUtils.copyProperties(banner,bannerResp);
            bannerResp.setPictureUrl(BaseResponse.httpstr(bannerResp.getPictureUrl()));
            bannerResps.add(bannerResp);
        }
        indexResp.setBannerList(bannerResps);
        // team
        List<Team> teamList = teamService.selectList(new Team());
        List<TeamResp> teamResps = new ArrayList<>();
        for (Team team:teamList) {
            TeamResp teamResp = new TeamResp();
            BeanUtils.copyProperties(team,teamResp);
            teamResp.setStaffimgurl(BaseResponse.httpstr(teamResp.getStaffimgurl()));
            teamResps.add(teamResp);
        }
        indexResp.setTeamResps(teamResps);
        // case
        List<Case> caseList = caseService.selectList(new Case());
        List<CaseResp> caseResps = new ArrayList<>();
        for (Case cas:caseList) {
            CaseResp caseResp = new CaseResp();
            BeanUtils.copyProperties(cas,caseResp);
            caseResp.setImage(BaseResponse.httpstr(caseResp.getImage()));
            caseResp.setLinkUrl(BaseResponse.httpstr(caseResp.getLinkUrl()));
            caseResps.add(caseResp);
        }
        indexResp.setCaseResps(caseResps);
        // cust
        List<Custservice> custserviceList = custserviceService.selectList(new Custservice());
        List<CustserviceResp> custserviceResps = new ArrayList<>();
        for (Custservice custservice:custserviceList) {
            CustserviceResp custserviceResp = new CustserviceResp();
            BeanUtils.copyProperties(custservice,custserviceResp);
            custserviceResp.setLogourl(BaseResponse.httpstr(custserviceResp.getLogourl()));
            custserviceResps.add(custserviceResp);
        }
        indexResp.setCustserviceResps(custserviceResps);
        response.setData(indexResp);
        // 记录访问
        savelog();
        return response;
    }

    private void savelog(){
        Visitlog visitlog = new Visitlog();
        String visitIP = AddressUtil.getIp(RequestHolder.getRequest());
        visitlog.setVisitIP(visitIP);
        String visittime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        visitlog.setVisittime(visittime);
        Visitlog visitlog1 = logService.selectOne(visitlog);
        if (visitlog1 == null){
            log.info("savelog ip :{}",visitIP);
            logService.insert(visitlog);
        }
    }

}
